package com.hongtech.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.hongtech.common.bean.AjaxResultVo;
import com.hongtech.common.page.PageData;
import com.hongtech.framework.anno.OperateLog;
import com.hongtech.framework.anno.Permission;
import com.hongtech.modules.common.BaseController;
import com.hongtech.modules.system.dto.SysDeptDTO;
import com.hongtech.modules.system.entity.SysDeptEntity;
import com.hongtech.modules.system.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 部门控制器
 */
@Controller
@RequestMapping("/system/sysdept")
public class DeptController extends BaseController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 跳转部门页
     */
    @Permission("system:sysdept:list")
    @RequestMapping("/list")
    public String listDept() {
        return "system/dept/list";
    }

    /**
     * 分页查询部门
     */
    @Permission("system:sysdept:list")
    @RequestMapping("/page")
    @ResponseBody
    public AjaxResultVo page(String search, Integer offset, Integer limit) {
        LambdaQueryWrapper<SysDeptEntity> ew = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            ew.like(SysDeptEntity::getDeptName, search);
        }
        Map<String, Object> pageParams = getPageParam(limit, offset);
        ew.orderByDesc(SysDeptEntity::getCreateTime);
        PageData<SysDeptDTO> pages = sysDeptService.pages(pageParams, ew);
        AjaxResultVo resultVo = AjaxResultVo.success();
        resultVo.put("total", pages.getTotal());
        resultVo.put("rows", pages.getList());
        return resultVo;
    }

    /**
     * 新增部门
     */
    @Permission("system:sysdept:save")
    @RequestMapping("/add")
    public String add() {
        return "system/dept/add";
    }

    /**
     * 执行新增
     */
    @Permission("system:sysdept:save")
    @OperateLog("创建部门")
    @RequestMapping("/doAdd")
    @ResponseBody
    public AjaxResultVo doAdd(SysDeptDTO dto) {
        sysDeptService.add(dto);
        return AjaxResultVo.success();
    }

    /**
     * 编辑部门
     */
    @Permission("system:sysdept:edit")
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        SysDeptDTO dept = sysDeptService.get(id);
        model.addAttribute("dept", dept);
        return "system/dept/edit";
    }

    /**
     * 执行编辑
     */
    @Permission("system:sysdept:edit")
    @OperateLog("编辑部门")
    @RequestMapping("/doEdit")
    @ResponseBody
    public AjaxResultVo doEdit(SysDeptDTO dto) {
        sysDeptService.update(dto);
        return AjaxResultVo.success();
    }

    /**
     * 删除部门
     */
    @Permission("system:sysdept:remove")
    @OperateLog("删除部门")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResultVo delete(Long id) {
        sysDeptService.delete(Lists.newArrayList(id));
        return AjaxResultVo.success();
    }

}
